package com.intsig.airflow.web.exception;

import com.intsig.airflow.web.ajax.RestResponseResult;
import org.springframework.validation.BindException;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author leojie 2021/3/13 10:25 下午
 */
@RestControllerAdvice
public class GlobalExceptionHandler extends BaseExceptionHandler{
    @Override
    @ExceptionHandler(AirflowWebDesignException.class)
    public RestResponseResult<String> serviceExceptionHandler(AirflowWebDesignException e) {
        String error = e.getMessage();
        return RestResponseResult.failed(error);
    }

    @Override
    @ExceptionHandler(Exception.class)
    public RestResponseResult<String> exceptionHandler(Exception e) {
        return super.exceptionHandler(e);
    }

    @ExceptionHandler({MethodArgumentNotValidException.class})
    public RestResponseResult<String> validException(MethodArgumentNotValidException e) {
        FieldError fieldError = e.getBindingResult().getFieldError();
        return super.validException(fieldError);
    }

    @Override
    @ExceptionHandler({BindException.class})
    public RestResponseResult<String> bindException(BindException e) {
        return super.bindException(e);
    }
}
